Regular Expressions problem

Hi,

I dont really get the usage of regular expressions, i've tried some things but i dont come to the correct solution.

I've got a module information (type: string) that is always structured like this:
"CTRS-DIA-118" or "CTRS-LEVMO-10"

I want to split this string into two strings, like this:

"CTRS-DIA-118" to -> "CTRS-DIA-" and "118"
"CTRS-LEVMO-10" to -> "CTRS-LEVMO-" and "10"

Can anyone please help me?

Regards,
Manuel
ManuelFelger - Fri Nov 13 11:26:49 EST 2009

Re: Regular Expressions problem
llandale - Fri Nov 13 12:54:21 EST 2009

Regexp    gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
                // Declare constant Regexp in main context,
                //      since 'regexp' is EXTREMELY inefficient and wasteful
 
//      Disect gl_reModParts:
//       [^-]                           any         char  except dash
//       [^-]+                          One or more chars except dash
//       [^-]+-                         One or more chars except dash, then a dash
//      ([^-]+-[^-]+-)  match 1, chars-dash-chars-dash
 
//       [0-9]+                 One or more digits
//      ([0-9]+)                match 2, one or more digits
//      ("^                        at start of string
//              $")                at end of string
 
bool    GetModParts(string NameMod, &BasePart, &NumberPart)
{     // Split the well formed module name into its parts,
        // Well Formed is:
        //      o exactly two dashes
        //      o characters before 1st dash
        //      o characters between dashes
        //      o only digits after 2nd dash
        // The BasePart   include everything up to 2nd dash
        // The NumberPart includes the digits
        //      e.g. Base-Part-22 has NumberPart "22"
        // If poorly formed, erase output strings and return false
 
        if (gl_reModParts NameMod)
        {  BasePart   = NameMod[match 1]
           NumberPart = NameMod[match 2]
           return(true)
        }
        else
        {  BasePart   = ""
           NumberPart = ""
           return(false)
        }
}     // end GetModParts()
 
void    Test_GetModParts(string NameMod)
{     string BasePart, NumberPart
 
        bool    Result = GetModParts(NameMod, BasePart, NumberPart)
        print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
 
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x")     // Bad, x at end
Test_GetModParts("CTRSDIA-118")       // Bad, only one dash
Test_GetModParts("a-b-c-44")          // Bad, 3 dashes
Test_GetModParts("-b-33")             // Bad, nothing before dash
Test_GetModParts("a--33")             // Bad, nothing between dashes
Test_GetModParts("a-b-")              // Bad, no numbers

 

 

  • Louie

 

 

Re: Regular Expressions problem
SystemAdmin - Mon Nov 16 03:20:44 EST 2009

llandale - Fri Nov 13 12:54:21 EST 2009

Regexp    gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
                // Declare constant Regexp in main context,
                //      since 'regexp' is EXTREMELY inefficient and wasteful
 
//      Disect gl_reModParts:
//       [^-]                           any         char  except dash
//       [^-]+                          One or more chars except dash
//       [^-]+-                         One or more chars except dash, then a dash
//      ([^-]+-[^-]+-)  match 1, chars-dash-chars-dash
 
//       [0-9]+                 One or more digits
//      ([0-9]+)                match 2, one or more digits
//      ("^                        at start of string
//              $")                at end of string
 
bool    GetModParts(string NameMod, &BasePart, &NumberPart)
{     // Split the well formed module name into its parts,
        // Well Formed is:
        //      o exactly two dashes
        //      o characters before 1st dash
        //      o characters between dashes
        //      o only digits after 2nd dash
        // The BasePart   include everything up to 2nd dash
        // The NumberPart includes the digits
        //      e.g. Base-Part-22 has NumberPart "22"
        // If poorly formed, erase output strings and return false
 
        if (gl_reModParts NameMod)
        {  BasePart   = NameMod[match 1]
           NumberPart = NameMod[match 2]
           return(true)
        }
        else
        {  BasePart   = ""
           NumberPart = ""
           return(false)
        }
}     // end GetModParts()
 
void    Test_GetModParts(string NameMod)
{     string BasePart, NumberPart
 
        bool    Result = GetModParts(NameMod, BasePart, NumberPart)
        print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
 
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x")     // Bad, x at end
Test_GetModParts("CTRSDIA-118")       // Bad, only one dash
Test_GetModParts("a-b-c-44")          // Bad, 3 dashes
Test_GetModParts("-b-33")             // Bad, nothing before dash
Test_GetModParts("a--33")             // Bad, nothing between dashes
Test_GetModParts("a-b-")              // Bad, no numbers

 

 

  • Louie

 

 

If you are using DOORS 9.1 or later the recommendation in the ref manual is to use regexp2 as in:

Regexp    gl_reModParts = regexp2("^([^-]+-[^-]+-)([0-9]+)$")

 


Not that I really understand why, but there has been some change in the RegExp engine!

/Kristian

 

Re: Regular Expressions problem
ManuelFelger - Mon Nov 16 07:02:24 EST 2009

llandale - Fri Nov 13 12:54:21 EST 2009

Regexp    gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
                // Declare constant Regexp in main context,
                //      since 'regexp' is EXTREMELY inefficient and wasteful
 
//      Disect gl_reModParts:
//       [^-]                           any         char  except dash
//       [^-]+                          One or more chars except dash
//       [^-]+-                         One or more chars except dash, then a dash
//      ([^-]+-[^-]+-)  match 1, chars-dash-chars-dash
 
//       [0-9]+                 One or more digits
//      ([0-9]+)                match 2, one or more digits
//      ("^                        at start of string
//              $")                at end of string
 
bool    GetModParts(string NameMod, &BasePart, &NumberPart)
{     // Split the well formed module name into its parts,
        // Well Formed is:
        //      o exactly two dashes
        //      o characters before 1st dash
        //      o characters between dashes
        //      o only digits after 2nd dash
        // The BasePart   include everything up to 2nd dash
        // The NumberPart includes the digits
        //      e.g. Base-Part-22 has NumberPart "22"
        // If poorly formed, erase output strings and return false
 
        if (gl_reModParts NameMod)
        {  BasePart   = NameMod[match 1]
           NumberPart = NameMod[match 2]
           return(true)
        }
        else
        {  BasePart   = ""
           NumberPart = ""
           return(false)
        }
}     // end GetModParts()
 
void    Test_GetModParts(string NameMod)
{     string BasePart, NumberPart
 
        bool    Result = GetModParts(NameMod, BasePart, NumberPart)
        print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
 
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x")     // Bad, x at end
Test_GetModParts("CTRSDIA-118")       // Bad, only one dash
Test_GetModParts("a-b-c-44")          // Bad, 3 dashes
Test_GetModParts("-b-33")             // Bad, nothing before dash
Test_GetModParts("a--33")             // Bad, nothing between dashes
Test_GetModParts("a-b-")              // Bad, no numbers

 

 

  • Louie

 

 

Works! Thank you very much Louie, without that function i probably never would have understood these regexp terms..

Regards,
Manuel

Re: Regular Expressions problem
SystemAdmin - Fri Jul 29 12:04:55 EDT 2011

llandale - Fri Nov 13 12:54:21 EST 2009

Regexp    gl_reModParts = regexp("^([^-]+-[^-]+-)([0-9]+)$")
                // Declare constant Regexp in main context,
                //      since 'regexp' is EXTREMELY inefficient and wasteful
 
//      Disect gl_reModParts:
//       [^-]                           any         char  except dash
//       [^-]+                          One or more chars except dash
//       [^-]+-                         One or more chars except dash, then a dash
//      ([^-]+-[^-]+-)  match 1, chars-dash-chars-dash
 
//       [0-9]+                 One or more digits
//      ([0-9]+)                match 2, one or more digits
//      ("^                        at start of string
//              $")                at end of string
 
bool    GetModParts(string NameMod, &BasePart, &NumberPart)
{     // Split the well formed module name into its parts,
        // Well Formed is:
        //      o exactly two dashes
        //      o characters before 1st dash
        //      o characters between dashes
        //      o only digits after 2nd dash
        // The BasePart   include everything up to 2nd dash
        // The NumberPart includes the digits
        //      e.g. Base-Part-22 has NumberPart "22"
        // If poorly formed, erase output strings and return false
 
        if (gl_reModParts NameMod)
        {  BasePart   = NameMod[match 1]
           NumberPart = NameMod[match 2]
           return(true)
        }
        else
        {  BasePart   = ""
           NumberPart = ""
           return(false)
        }
}     // end GetModParts()
 
void    Test_GetModParts(string NameMod)
{     string BasePart, NumberPart
 
        bool    Result = GetModParts(NameMod, BasePart, NumberPart)
        print Result "\t[" BasePart "]\t#" NumberPart "\t*" NameMod "*\n"
}
 
Test_GetModParts("CTRS-DIA-118")
Test_GetModParts("1-2-03")
Test_GetModParts("CTRS-DIA-118x")     // Bad, x at end
Test_GetModParts("CTRSDIA-118")       // Bad, only one dash
Test_GetModParts("a-b-c-44")          // Bad, 3 dashes
Test_GetModParts("-b-33")             // Bad, nothing before dash
Test_GetModParts("a--33")             // Bad, nothing between dashes
Test_GetModParts("a-b-")              // Bad, no numbers

 

 

  • Louie

 

 

Just curious, what do you do if your first character is a space?
Or are you just going to return it false?

Re: Regular Expressions problem
llandale - Mon Aug 01 00:22:02 EDT 2011

SystemAdmin - Fri Jul 29 12:04:55 EDT 2011
Just curious, what do you do if your first character is a space?
Or are you just going to return it false?

Yes regexp2.

At home but I'm pretty sure it will pass if the 1st character is a space. The code gets tricky if you must not have any white space. I'd add a separater check for that. Search for "fWhite" in the forums, surely I've posted code that does that check.

  • Louie